import {formatMessage} from './intl';
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);
/* eslint-disable prefer-const, no-bitwise, no-mixed-operators */

export const uuid = () =>
    'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        let r = (Math.random() * 16) | 0,
            v = c === 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
export const validateAdd = (data, id) => {
    const {dealerId, typeId, details = []} = data;
    const msgs = [];
    if(!typeId)
        msgs.push(
            formatMessage({
                id: 'utils.typeId',
                defaultMessage: '订单类型'
            })
        );
    if(!id && !dealerId)
        msgs.push(
            formatMessage({
                id: 'utils.dealer',
                defaultMessage: '总代'
            })
        );
    if(!details.length)
        msgs.push(
            formatMessage({
                id: 'utils.details',
                defaultMessage: '清单'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    const partCodes = {};
    for(let i = 0; i < details.length; i++) {
        const {partCode, planQuantity, taxPrice, minPackSize} = details[i];
        if(!planQuantity || !taxPrice)
            return formatMessage({
                id: 'utils.detail.validate1',
                defaultMessage: '清单中的订货数量、含税金额必须大于0'
            });
        else if(planQuantity && minPackSize && !Number.isInteger(planQuantity / minPackSize))
            return formatMessage({
                id: 'utils.detail.validate2',
                defaultMessage: '备件最小包装数大于0时，备件清单的订货数量是最小包装数的整数倍'
            });
        else if(partCodes[partCode])
            return formatMessage({
                id: 'utils.detail.validate3',
                defaultMessage: '备件清单列表存在重复的备件编号'
            });
        partCodes[partCode] = true;
    }
    return false;
};
import {PERMISSION} from './constants';
export const isSubmitable = (data = [], options = [], url, id) => {
    if(!url) return false;
    if(id) return data.includes(PERMISSION[url]) && options.includes(PERMISSION[url]);
    return data.includes(PERMISSION[url]);
};
